﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace LogikBug
{
	/// <summary>
	/// 
	/// </summary>
    public static class StringExtensions
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static bool IsNotValid(this string value)
		{
			return string.IsNullOrEmpty(value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static bool IsNotNullOrEmpty(this string value)
		{
			return !string.IsNullOrEmpty(value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public static void ThrowIfNullOrEmpty(this string value)
		{
			value.ThrowIfNullOrEmpty("value");
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="paramName"></param>
		/// <exception cref="ArgumentException"><c>ArgumentException</c>.</exception>
		public static void ThrowIfNullOrEmpty(this string value, string paramName)
		{
			value.ThrowIfNull(paramName);

			if (value.Length == 0)
			{
				throw new ArgumentException(string.Format("Value cannot be an empty string. parameter name: '{0}'", paramName), paramName);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="result"></param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException">Argument is null.</exception>
		public static bool GuidTryParse(this string value, out Guid result)
		{
			if (value.IsNull())
			{
				throw new ArgumentNullException("value");
			}

			try
			{
				result = new Guid(value);
				return true;
			}
			catch (FormatException)
			{
				result = Guid.Empty;
				return false;
			}
			catch (OverflowException)
			{
				result = Guid.Empty;
				return false;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static string ComputeHash(this string value)
		{
			StringBuilder sb = new StringBuilder();

			if (value.IsNotNullOrEmpty())
			{
				MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
				byte[] data = Encoding.ASCII.GetBytes(value);
				data = md5.ComputeHash(data);

				for (int i = 0; i < data.Length; i++)
				{
					sb.Append(data[i].ToString("x2"));
				}
			}

			return sb.ToString();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="func"></param>
		/// <returns></returns>
		public static string Substring(this string value, Func<string, int> func)
		{
			return value.Substring(0, func);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		/// <param name="startIndex"></param>
		/// <param name="func"></param>
		/// <returns></returns>
		public static string Substring(this string value, int startIndex, Func<string, int> func)
		{
			return value.Substring(startIndex, func(value));
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="text"></param>
		/// <param name="start"></param>
		/// <param name="end"></param>
		/// <returns></returns>
		public static string SubstringRange(this string text, int start, int end)
		{
			return text.Substring(start, end - start);
		}
    }
}